package kk.controller;

import com.github.pagehelper.PageInfo;
import kk.domain.Permission;
import kk.domain.Role;
import kk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/findAll")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ModelAndView findAll(Integer pageNum,Integer pageSize) throws Exception {
        ModelAndView modelAndView=new ModelAndView();
        List<Role> roleList = roleService.findAll(pageNum,pageSize);
        PageInfo pageInfo=new PageInfo(roleList);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("role-list");
        return modelAndView;
    }

    @RequestMapping("/save")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String save(Role role) throws Exception{
        roleService.save(role);
        return "redirect:/role/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ModelAndView findById(Integer id) throws Exception{
        Role role = roleService.findById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("role",role);
        modelAndView.setViewName("role-show");
        return modelAndView;
    }

    @RequestMapping("/deleteRole")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String deleteRole(Integer id) throws Exception {
        roleService.deleteRole(id);
        return "redirect:/role/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/findRoleByIdAndAllPermission")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ModelAndView findRoleByIdAndAllPermission(@RequestParam(name = "id",required = true) Integer roleId) throws Exception {
        Role role = roleService.findById(roleId);
        List<Permission> permissionList = roleService.findOtherPermission(roleId);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("role",role);
        modelAndView.addObject("permissionList",permissionList);
        modelAndView.setViewName("role-permission-add");
        return modelAndView;
    }

    @RequestMapping("/findRoleByIdAndCanDeletePermission")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ModelAndView findRoleByIdAndCanDeletePermission(@RequestParam(name = "id",required = true) Integer roleId) throws Exception{
        Role role = roleService.findById(roleId);
        List<Permission> permissionList = roleService.findCanDeletePermission(roleId);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("role",role);
        modelAndView.addObject("permissionList",permissionList);
        modelAndView.setViewName("role-permission-delete");
        return modelAndView;
    }

    @RequestMapping("/addPermissionToRole")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String addPermissionToRole(Integer roleId,int[] ids) throws Exception {
        roleService.addPermissionToRole(roleId,ids);
        return "redirect:/role/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/deletePermissionFromRole")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String deletePermissionFromRole(Integer roleId,int[] ids) throws Exception{
        roleService.deletePermissionFromRole(roleId,ids);
        return "redirect:/role/findAll?pageNum=1&pageSize=4";
    }

    @RequestMapping("/deleteSelectedRole")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String deleteSelectedRole(int[] ids) throws Exception{
        for (int id : ids) {
            roleService.deleteRole(id);
        }
        return "redirect:/role/findAll?pageNum=1&pageSize=4";
    }
}
